package com.wjx;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Producer {

    @Test
    public void response() throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Map<String, Object> serviceInstances = new HashMap<>();
        serviceInstances.put(UserService.class.getName(), new UserServiceImpl());
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("D:\\王建新\\Desktop\\request.data"));
        String interfaceName = objectInputStream.readUTF();
        String methodName = objectInputStream.readUTF();
        Class<?>[] parameterTypes = (Class<?>[])objectInputStream.readObject();
        Object[] args = new Object[parameterTypes.length];
        for (int i = 0; i < args.length; i++) {
            args[i] = objectInputStream.readObject();
        }
        Object instance = serviceInstances.get(interfaceName);
        Method declaredMethod = instance.getClass().getDeclaredMethod(methodName, parameterTypes);
        declaredMethod.setAccessible(true);
        System.out.println(declaredMethod.invoke(instance, args));
    }
}
